﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using RB2015.DAL;
using System.Text;
using System.Net.Mail;
using RB2015.Models;
namespace RB2015.Controllers
{
    public class HomeController : Controller
    {
        //
        // GET: /Home/
        //private ArticleDataContext dataBase = new ArticleDataContext();
        public ActionResult Index()
        {
            ViewBag.CurrentPage = "home";
            return View();
        }

        public ActionResult Resume()
        {
            ViewBag.CurrentPage = "resume";
            return View();
        }

        public ActionResult Contact()
        {
            ViewBag.CurrentPage = "contact";
            //PopulateDropDownList();
            return View();
        }

        public ActionResult Articles()
        {
            ViewBag.CurrentPage = "articles";
            return View();
        }

        //public void PopulateDropDownList()
        //{
        //    var query = from d in dataBase.Articles
        //                orderby d.ArticleId
        //                select new { d.ArticleId, d.ArticleName };
        //    ViewBag.Articles = new SelectList(query.AsEnumerable(), "ArticleId", "ArticleName");
        //}

        //public string GetArticleNameFromSelectedValue(int articleId)
        //{
        //    var articleName = from d in dataBase.Articles
        //                      where d.ArticleId == articleId
        //                      select d.ArticleName;
        //    var selectedItem = articleName.First();
        //    return selectedItem;
        //}

        [HttpPost]
        public ActionResult Contact(Articles a)
        {
            if (ModelState.IsValid)
            {
                StringBuilder sb = new StringBuilder();
                MailMessage msg = new MailMessage();
                SmtpClient smtp = new SmtpClient();
                MailAddress address = new MailAddress("ryan@ryanbutler.org");
                MailAddress from = new MailAddress(a.Email.ToString());
                msg.Subject = "Contact Me";
                msg.To.Add(address);
                msg.From = from;
                smtp.Host = "webmail.ryanbutler.org";
                smtp.Port = 587;
                msg.IsBodyHtml = false;
                sb.Append(Environment.NewLine);
                sb.Append("First name:" + a.FirstName);
                sb.Append(Environment.NewLine);
                sb.Append("Last name:" + a.LastName);
                sb.Append(Environment.NewLine);
                sb.Append("Email:" + a.Email);
                //GetArticleNameFromSelectedValue(a.ArticleId);
                sb.Append(Environment.NewLine);
                //ArticleQuestionYes(sb, a);
                sb.Append("Comments: " + a.Comments);
                msg.Body = sb.ToString();
                try
                {
                    smtp.Send(msg);
                    smtp.Dispose();
                    msg.Dispose();
                    ViewBag.CurrentPage = "contact";
                    return View("Message");
                }
                catch (Exception ex)
                {
                    throw new Exception(ex.ToString());
                    //return View("Error");
                }
            }
            ViewBag.CurrentPage = "contact";
            return View();
        }

        //public string ArticleQuestionYes(StringBuilder sb, Articles a)
        //{
        //    sb.Append("Article question? " + "Yes");
        //    //string selectedItem = GetArticleNameFromSelectedValue(a.ArticleId);
        //    sb.Append(Environment.NewLine);
        //    //sb.Append("Which article? " + selectedItem.ToString());
        //    sb.Append(Environment.NewLine);
        //    return sb.ToString();
        //}
        //public string ArticleQuestionNo(StringBuilder sb)
        //{
        //    sb.Append("Article question? " + "No");
        //    sb.Append(Environment.NewLine);
        //    return sb.ToString();
        //}

        //protected override void Dispose(bool disposing)
        //{
        //    dataBase.Dispose();
        //    base.Dispose(disposing);
        //}

    }
}
